﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MemoBoss
{
    public partial class Card : UserControl
    {
        //internal Image _cardCover;

        //internal Image _cardImage;

        //private bool _contentLoaded;

        //internal Image _lockedCard;

        //internal Storyboard Flip;

        //internal Storyboard FlipBack;

        //internal Storyboard GoodMatch;

        //internal Grid LayoutRoot;

        //internal Rectangle rectangle;

        //internal Storyboard Reset;

        public bool Flipping
        {
            get;
            set;
        }

        public int ImageId
        {
            get;
            set;
        }

        public bool IsSealed
        {
            get;
            set;
        }

        public bool Locked
        {
            get;
            set;
        }

        public Card()
        {

            this.InitializeComponent();
            //base.MouseLeftButtonUp += OnCardCoverTap;
            //this.Flip.Completed += (OnFlipCompleted);
            //this.FlipBack.Completed += (OnFlipBackCompleted);
            //this.GoodMatch.Completed += (OnGoodMatchCompleted);


           

        }

        public void FlipCard()
        {
            if (!this.IsSealed)
            {
                this.Flipping = true;
                this.Flip.Begin();
                return;
            }
            else
            {
                return;
            }
        }


        public void MatchCard(bool ismatch)
        {
            if (!ismatch)
            {
                this.Flipping = false;
                this.FlipBack.Begin();
               
                return;
            }
            else
            {
                this.Locked = true;
                this.GoodMatch.Begin();
               
                return;
            }
        }

        

        private void OnFlipBackCompleted(object sender, EventArgs e)
        {
            this.Flipping = false;
        }

        private void OnFlipCompleted(object sender, EventArgs e)
        {
            if (this.CardFlipped != null)
            {
                this.CardFlipped(this, null);
            }
        }

        private void OnGoodMatchCompleted(object sender, EventArgs e)
        {
            this.GoodMatchDone(this, null);
        }

        //public void SealCard()
        //{
        //    this.IsSealed = true;
        //    this._lockedCard.Visibility = Visibility.Visible;
        //}

        //public void UnsealCard()
        //{
        //    if (this.IsSealed)
        //    {
        //        this.IsSealed = false;
        //        this._lockedCard.Visibility = Visibility.Collapsed;
        //        return;
        //    }
        //    else
        //    {
        //        return;
        //    }
        //}

        public event EventHandler BeforeCardFlip;
        public event EventHandler CardFlipped;
        public event EventHandler GoodMatchDone;

     

        private void Card_Tap(object sender, GestureEventArgs e)
        {
            if (this.Flipping || this.Locked)
            {
                return;
            }
            else
            {
                this.BeforeCardFlip(this, null);
                return;
            }
        }

        private void btnOptionsGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Push.Stop();
            //Push.Children.ToList().ForEach(a => Storyboard.SetTarget(a, (DependencyObject)sender));
            Push.Begin();
        }

        private void btnOptionsGrid_MouseLeave(object sender, MouseEventArgs e)
        {
            //Release.Stop();
            //Release.Children.ToList().ForEach(a => Storyboard.SetTarget(a, (DependencyObject)sender));
            Release.Begin();
        }
    }
}
